<?php
namespace app\sports\match\model;

use think\Db;
use think\Model;

class JCModel extends Model
{
    private $database = 'sports_database';

    /**
     * 获取列表
     */
    public function getJCList($type, $page = 1, $count = 10)
    {
        $list = Db::database($this->database)->table('foot_ball_jc')->where(['type' => $type])->where('match_time', '>', time());
        $jcList = $list->order('match_time', 'asc')->page($page, $count)->select();
        $count = $list->count();
        return ['code' => 0, 'data' => ['list' => $jcList, 'count' => (int)$count]];
    }

    /**
     * 根据nami_id获取关联数据
     */
    public function getJCLinkListByNaMiId($type, $namiId)
    {
        $jcLink = \Db::database($this->database)->table('foot_ball_jc_link')->where(['lottery_type' => $type])->whereIn('nami_id', $namiId)->select();
        $jcLinkRes = [];
        foreach ($jcLink as $item) {
            $jcLinkRes[$item['nami_id']] = $item;
        }

        return ['code' => 0, 'data' => ['list' => $jcLinkRes]];
    }

    /**
     * 根据match_id获取关联数据(批量)
     */
    public function getJCLinkListByMatchId($type, $matchId)
    {
        $jcLink = \Db::database($this->database)->table('foot_ball_jc_link')->where(['lottery_type' => $type])->whereIn('match_id', $matchId)->select();
        $jcLinkRes = [];
        foreach ($jcLink as $item) {
            $jcLinkRes[$item['match_id']] = $item;
        }

        return ['code' => 0, 'data' => ['list' => $jcLinkRes]];
    }

    /**
     * 根据nami_id获取竞彩数据(批量)
     */
    public function getJcListByNaMiId($type, $namiId)
    {
        $jcList = Db::database($this->database)->table('foot_ball_jc')->where('type', $type)
            ->whereIn('nami_id', $namiId)->select();
        return ['code' => 0, 'data' => ['list' => $jcList]];
    }

    /**
     * 根据match_id获取关联数据(单条)
     */
    public function getJCLinkByMatchId($type, $matchId)
    {
        $jcLink = \Db::database($this->database)->table('foot_ball_jc_link')->where(['lottery_type' => $type])->whereIn('match_id', $matchId)->find();

        return ['code' => 0, 'data' => $jcLink];
    }

    /**
     * 根据nami_id获取竞彩数据(单条)
     */
    public function getJcByNaMiId($type, $namiId)
    {
        $jcList = Db::database($this->database)->table('foot_ball_jc')->where('type', $type)
            ->where('nami_id', $namiId)->find();

        return ['code' => 0, 'data' => $jcList];
    }
}